﻿package game {
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import game.ui.MouseHelp;
	import fl.lang.Locale;
	import game.debug.Debug;
	
	public class GameMouse {
		
		private var stage:Stage;
		
		public function GameMouse (stage:Stage):void {
			this.stage = stage;
			
			stage.addEventListener(MouseEvent.MOUSE_OVER,	mOver);
			stage.addEventListener(MouseEvent.MOUSE_OUT,	mOut);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,	mMove);
		}
		
		private function mOver(event:MouseEvent):void {
			mOut(null);
			
			var str:String = event.target.name as String;
			var ignoreName:String = "instance";
			if (str.substr(0, ignoreName.length)==ignoreName) return;
			
			str = "IDS_"+str.toLocaleUpperCase();
			
			var n:String = Locale.loadString(str);
			
			if (n) {
				var help:MouseHelp = new MouseHelp(n);
				help.name = "mouseHelp";
				stage.addChild(help);
			} else {
				Debug.log("Ошибка MouseHelp: "+str, Debug.HELP);
				return;
			}
			
			mMove(null);
		}
		
		private function mOut(event:MouseEvent):void {
			if (!stage) return;
			
			var mouseHelp:MouseHelp = stage.getChildByName('mouseHelp') as MouseHelp;
			if (mouseHelp) {
				if (event)
					mouseHelp.close();
				else 
					stage.removeChild(mouseHelp);
			}
		}
		
		private function mMove(event:MouseEvent):void {
			if (!stage) return;
			
			var mouseHelp:MouseHelp = stage.getChildByName('mouseHelp') as MouseHelp;
			if (mouseHelp) {
				var _x:Number = stage.mouseX;
				var _y:Number = stage.mouseY;
				_y = _y - mouseHelp.height - 2;
				if (_y<0) _y = 0;
				if (_x+mouseHelp.width>stage.stageWidth) _x = stage.stageWidth - mouseHelp.width;
				mouseHelp.move(_x, _y);
			}
		}
	}
}